package handler

import (
	"context"
	"eat-server/global"
	"eat-server/models"
	"eat-server/pkg"
	__ "eat-server/proto"
	"fmt"
	"github.com/google/uuid"
)

func (s *Server) GoodList(_ context.Context, in *__.GoodListRequest) (*__.GoodListResponse, error) {
	var find []models.Goods
	global.DB.Find(&find)
	var finds []*__.List
	for _, v := range find {
		list := __.List{

			GoodsPrice:  float32(v.GoodsPrice),
			GoodsMs:     v.GoodsMs,
			GoodsCover:  v.GoodsCover,
			GoodsStatus: v.GoodsStatus,
			GoodsLike:   v.GoodsLike,
		}
		finds = append(finds, &list)
	}
	return &__.GoodListResponse{
		List: finds,
	}, nil
}
func (s *Server) AddOrder(_ context.Context, in *__.AddOrderRequest) (*__.AddOrderResponse, error) {
	g := models.Goods{}
	err := g.Info(int(in.Goods_Id))
	if err != nil {
		return nil, fmt.Errorf("商品不存在")
	}
	orderSn := uuid.NewString()
	amount := g.GoodsPrice * float64(in.Num)
	o := models.Orders{
		UserId:  in.Userid,
		OrderSn: orderSn,
		PayType: int8(in.PayType),
		Status:  1,
		PayTime: 1,
		Amount:  amount,
	}
	err = o.AddOrder()
	
	if err != nil {
		return nil, fmt.Errorf("订单创建失败")
	}
	or := models.OrderGoods{
		OrderId:    int64(o.Id),
		GoodsId:    in.Goods_Id,
		Num:        in.Num,
		Price:      g.GoodsPrice,
		GoodsCover: g.GoodsCover,
	}
	err = or.AddOrderGoods()
	if err != nil {
		return nil, fmt.Errorf("订单商品创建失败")
	}

	totalAmount := fmt.Sprintf("%.2F", amount)
	alipay := pkg.AliPay{
		Subject:     "商品支付",
		OutTradeNo:  orderSn,
		TotalAmount: totalAmount,
	}
	url := alipay.Pay()

	return &__.AddOrderResponse{
		OrderId: int64(o.Id),
		Url:     url,
	}, nil
}
func (s *Server) OrderList(_ context.Context, in *__.OrderListRequest) (*__.OrderListResponse, error) {
	page := in.Page
	if page <= 0 {
		page = 1
	}
	pageSize := in.Size
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}
	offset := (page - 1) * pageSize
	var find []models.OrderGoods
	global.DB.Offset(int(offset)).Limit(int(pageSize)).Find(&find)
	var finds []*__.Order
	for _, v := range find {
		list := __.Order{
			OrderId:    v.OrderId,
			GoodsId:    v.GoodsId,
			Num:        v.Num,
			Price:      float32(v.Price),
			GoodsTitle: v.GoodsTitle,
			GoodsCover: v.GoodsCover,
		}
		finds = append(finds, &list)
	}
	return &__.OrderListResponse{
		List: finds,
	}, nil
}
func (s *Server) OrderInfo(_ context.Context, in *__.OrderInfoRequest) (*__.OrderInfoResponse, error) {
	var find models.OrderGoods
	global.DB.Find(&find)
	if find.Id == 0 {
		return nil, fmt.Errorf("商品不存在 不允许查看")
	}
	finds := models.OrderGoods{
		Id: uint64(in.Id),
	}
	global.DB.Find(&finds)
	return &__.OrderInfoResponse{
		OrderId:    finds.OrderId,
		GoodsId:    finds.GoodsId,
		Num:        finds.Num,
		Price:      float32(finds.Price),
		GoodsTitle: finds.GoodsTitle,
		GoodsCover: finds.GoodsCover,
	}, nil
}
func (s *Server) OrderDel(_ context.Context, in *__.OrderDelRequest) (*__.OrderDelResponse, error) {
	var find models.OrderGoods
	global.DB.Find(&find)
	if find.Id == 0 {
		return nil, fmt.Errorf("商品不存在 不允许删除")
	}
	finds := models.OrderGoods{
		Id: uint64(in.Id),
	}
	global.DB.Delete(&finds)
	return &__.OrderDelResponse{}, nil
}
